﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using QLab.Model;
using System.Collections.ObjectModel;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
using QLab.Events;

namespace QLab.Controllers.VariableController
{
	[Export]
	[PartCreationPolicy(CreationPolicy.Shared)]
	public class VariableController : NotificationObject
	{
		private ObservableCollection<VariableItem> _variables;

		private IEventAggregator _eventAggregator;
		private IRegionManager _regionManager;

		[ImportingConstructor]
		public VariableController(IEventAggregator EventAggregator, IRegionManager RegionManager)
		{
			_regionManager = RegionManager;
			_eventAggregator = EventAggregator;

			_variables = new ObservableCollection<VariableItem>();

			_eventAggregator.GetEvent<CommandExecutedVars>().Subscribe(OnCommandExecutedVars);
		}

		#region Properties

		public ObservableCollection<VariableItem> Variables { get { return _variables; } }

		#endregion

		#region Callbacks

		public void OnCommandExecutedVars(Dictionary<string, string> Variables)
		{
			_variables.Clear();
			foreach (string name in Variables.Keys)
			{
				_variables.Add(new VariableItem() { Value = Variables[name], Name = name });
			}
		}

		#endregion
	}
}
